package com.gitee.mars.marsquickframe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.marsquickframe.entity.NoteBook;
import com.gitee.mars.marsquickframe.request.FavoriteRequest;
import com.gitee.mars.marsquickframe.response.base.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.mars.marsquickframe.service.IFavoriteService;
import com.gitee.mars.marsquickframe.mapper.FavoriteMapper;
import com.gitee.mars.marsquickframe.entity.Favorite;
import org.springframework.util.CollectionUtils;

import java.util.Date;


/**
 * Favorite 实现层
 *
 * @Author wq
 * @Date 2021-05-04
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements IFavoriteService {


    @Override
    public void add(FavoriteRequest request) {
        Favorite favorite = new Favorite();
        favorite.setContent(request.getContent());
        favorite.setCreateTime(new Date());
        favorite.setUpdateTime(new Date());
        save(favorite);
    }

    @Override
    public PageInfo<Favorite> list(FavoriteRequest request) {
        IPage<Favorite> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(request.getContent())) {
            wrapper.like(Favorite::getContent, request.getContent());
        }
        IPage<Favorite> sysUserIPage = baseMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(sysUserIPage.getRecords())) {
            return new PageInfo<>(page.getTotal(), sysUserIPage.getRecords());
        }
        return null;
    }

    @Override
    public void delete(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void update(FavoriteRequest request) {
        Favorite favorite = new Favorite();
        BeanUtils.copyProperties(request, favorite);
        updateById(favorite);
    }

    @Override
    public Favorite detail(Integer id) {
        return baseMapper.selectById(id);
    }
}